//
//  MNDTipsViewController.m
//  minidai
//
//  Created by 容贤郁 on 2017/5/30.
//  Copyright © 2017年 迷你金服. All rights reserved.
//

#import "MNDTipsViewController.h"
#import "UIButton+JKImagePosition.h"
#import "UIImage+JKColor.h"

@interface MNDTipsViewController ()

@property (strong, nonatomic) UIView *translucentView;

@end

@implementation MNDTipsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(closeAction:)];
    [self.view addGestureRecognizer:tapGesture];
    
    if ([self.type isEqualToString:tipsCalendarDetail]) {
        self.view.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.7];
        [self initialTipsContents];
        return;
    }
    self.view.backgroundColor = [[UIColor clearColor] colorWithAlphaComponent:0.0];
    
    UIImage *backGroundImage = [UIImage jk_imageWithColor:[[UIColor blackColor] colorWithAlphaComponent:1.0]];
    UIImageView *backGroundImageView = [[UIImageView alloc] initWithImage:backGroundImage];
    
    [self.view addSubview:backGroundImageView];
    [backGroundImageView setFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight)];
    //挖出一片空白区域
    CGRect bounds = [UIScreen mainScreen].bounds;
    
    UIBezierPath *path = [UIBezierPath bezierPathWithRect:CGRectMake(0, 0, bounds.size.width, bounds.size.height)];
    
    UIBezierPath *innerPath = [UIBezierPath bezierPathWithRect:self.transparentFrame];
    [path appendPath:innerPath];
    [path setUsesEvenOddFillRule:YES];
    
    CAShapeLayer *fillLayer = [CAShapeLayer layer];
    fillLayer.path = path.CGPath;
    
    fillLayer.fillRule = kCAFillRuleEvenOdd;
    fillLayer.fillColor = [[UIColor blackColor] colorWithAlphaComponent:0.8].CGColor;
    
    [backGroundImageView.layer setMask:fillLayer];
    
    [self initialTipsContents];
}

- (void)initialTipsContents {
    

    self.translucentView = [[UIView alloc] initWithFrame:self.transparentFrame];
    self.translucentView.backgroundColor = [UIColor clearColor];
    [self.view addSubview:self.translucentView];
    
    UIImageView *tipsImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"tips_home_forum"]];

    [self.view addSubview:tipsImageView];
    
    UIButton *tipsButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [tipsButton setTitle:@"我知道了" forState:UIControlStateNormal];
    [tipsButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    tipsButton.titleLabel.font = [UIFont systemFontOfSize:14.0];
    [tipsButton setImage:[UIImage imageNamed:@"tips_close_btn"] forState:UIControlStateNormal];
    [tipsButton jk_setImagePosition:LXMImagePositionRight spacing:5];
    
    [self.view addSubview:tipsButton];
    
    [tipsButton addTarget:self action:@selector(closeAction:) forControlEvents:UIControlEventTouchUpInside];
    
    [self makConstraintwith:tipsImageView button:tipsButton];
}

- (void)makConstraintwith:(UIImageView *)tipsImageView button:(UIButton *)tipsButton {
    
    //论坛提示
    if ([self.type isEqualToString:tipsHomeForum]) {
        [tipsImageView setImage:[UIImage imageNamed:@"tips_home_forum"]];
        
        [tipsImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.bottom.equalTo(self.translucentView.mas_top).offset(-20);
            if (kScreenWidth > 375) {
                make.centerX.equalTo(self.translucentView.mas_centerX).offset(-52);
            } else {
                make.centerX.equalTo(self.translucentView.mas_centerX).offset(-48);
            }
        }];
        
        [tipsButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.bottom.equalTo(tipsImageView.mas_top).offset(-15.0);
            make.trailing.equalTo(tipsImageView);
        }];
    }
    
    //首页资产卡片
    if ([self.type isEqualToString:tipsHomeAsset]) {
        
        [tipsImageView setImage:[UIImage imageNamed:@"tips_home_asset"]];
        
        [tipsImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.bottom.equalTo(self.translucentView.mas_top).offset(-20);

            make.centerX.equalTo(self.translucentView);
        }];
        
        [tipsButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.bottom.equalTo(tipsImageView.mas_top).offset(-15.0);
            make.trailing.equalTo(tipsImageView);
        }];
    }
    
    //投资列表
    if ([self.type isEqualToString:tipsFinanceLits]) {
        
        [tipsImageView setImage:[UIImage imageNamed:@"tips_finance_list"]];
        
        [tipsImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.translucentView.mas_bottom).offset(20);
            
            make.centerX.equalTo(self.translucentView);
        }];
        
        [tipsButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(tipsImageView.mas_bottom).offset(15.0);
            make.trailing.equalTo(tipsImageView);
        }];
    }
    
    //积分兑换
    if ([self.type isEqualToString:tipsForumIntegral]) {
        [tipsImageView setImage:[UIImage imageNamed:@"tips_forum"]];
        
        [tipsImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.translucentView.mas_bottom).offset(20);
            
            make.centerX.equalTo(self.translucentView);
        }];
        
        [tipsButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(tipsImageView.mas_bottom).offset(15.0);
            make.trailing.equalTo(tipsImageView);
        }];
    }
    //用户中心总资产
    if ([self.type isEqualToString:tipsMineAsset]) {
        [tipsImageView setImage:[UIImage imageNamed:@"tips_asset_count"]];
        
        [tipsImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.translucentView.mas_bottom).offset(20);
            
            make.leading.equalTo(self.view).offset(30);
        }];
        
        [tipsButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(tipsImageView.mas_bottom).offset(15.0);
            make.trailing.equalTo(tipsImageView);
        }];
    }
    
    //用户中心账单
    if ([self.type isEqualToString:tipsMineRecord]) {
        [tipsImageView setImage:[UIImage imageNamed:@"tips_record"]];
        
        [tipsImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.translucentView.mas_bottom).offset(20);
            
            make.trailing.equalTo(self.view).offset(-20);
        }];
        
        [tipsButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(tipsImageView.mas_bottom).offset(15.0);
            make.trailing.equalTo(self.view).offset(-30);
        }];
    }
    
    //用户中心最近30天收益
    if ([self.type isEqualToString:tipsMineIncome]) {
        [tipsImageView setImage:[UIImage imageNamed:tipsMineIncome]];
        
        [tipsImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.translucentView.mas_bottom).offset(20);
            
            make.leading.equalTo(self.view).offset(30);
        }];
        
        [tipsButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(tipsImageView.mas_bottom).offset(15.0);
            make.trailing.equalTo(tipsImageView);
        }];
    }
    
    //首页最近30天收益
    if ([self.type isEqualToString:tipsHomeIncome]) {
        [tipsImageView setImage:[UIImage imageNamed:tipsHomeIncome]];
        
        [tipsImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.bottom.equalTo(self.translucentView.mas_top).offset(-20);
            
            make.leading.equalTo(self.translucentView);
        }];
        
        [tipsButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.bottom.equalTo(tipsImageView.mas_top).offset(-15.0);
            make.trailing.equalTo(tipsImageView);
        }];
    }
    
    //回款日历
    if ([self.type isEqualToString:tipsCalendar]) {
        
        [tipsImageView setImage:[UIImage imageNamed:tipsCalendar]];
        
        [tipsImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.translucentView.mas_bottom).offset(20);
            
            make.centerX.equalTo(self.translucentView);
        }];
        
        [tipsButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(tipsImageView.mas_bottom).offset(15.0);
            make.trailing.equalTo(tipsImageView);
        }];
    }
    
    if ([self.type isEqualToString:tipsCalendarDetail]) {
        
        [self.translucentView setHidden:YES];
        
        [tipsImageView setImage:[UIImage imageNamed:tipsCalendarDetail]];
        
        [tipsImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.view.mas_centerY).offset(-50);
            make.centerX.equalTo(self.view);
        }];
        
        [tipsButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(tipsImageView.mas_bottom).offset(40.0);
            make.centerX.equalTo(tipsImageView);
        }];
    }
}

- (void)closeAction:(id)sender {
    
    if (self.closeTipsDidClick) {
        self.closeTipsDidClick();
    }
    [self.view removeFromSuperview];
    [self removeFromParentViewController];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
